#include "admanager.h"

#include "../sleipner_config.h"

#ifdef _PLATFORM_IOS
#include "c_admanager.h"
#endif

#ifdef _PLATFORM_ANDROID
#include "SDL2/src/core/android/SDL_android.h"
#include "android/sleipner_android.h"
#endif



static void     OnFullScreenCB(void);
static void     OnDismissCB(void);

AdManager&          AdManager::Instance()
{
    static AdManager	Inst;
	return Inst;
}

AdManager::AdManager() : m_pHandler(NULL)
{
    
}

void     AdManager::AdInit (const std::string& BannerAdKey,
                          const std::string& FullScreenAdAppID,
                          const std::string& FullScreenAdKey,
                          AdHandler* pHandler
                                )
{
    m_pHandler = pHandler;
    
#ifdef _PLATFORM_IOS
     CAdInit (BannerAdKey.c_str(),
              FullScreenAdAppID.c_str(),
              FullScreenAdKey.c_str(),
               (OnPresentFullScreenModal)OnFullScreenCB, 
               (OnDismissFullScreenModal)OnDismissCB);
#endif
    
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdInit, 0);
#endif

}

void	AdManager::HideBannerAd()
{
#ifdef _PLATFORM_IOS
    CAd_Hide();
#endif
    
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdCloseBanner, 0);	
#endif
}

void	AdManager::RequestFullScreenAd(int Type)
{
#ifdef _PLATFORM_IOS
    CRequestFullScreenAd();
#endif
    
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdRequestFullScreen, Type);	
#endif
}


void	AdManager::RequestBannerAd()
{
    
#ifdef _PLATFORM_IOS
    CAd_Show();
#endif
    
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdRequestBanner, 0);	
#endif
}

void     AdManager::ShutDown(void)
{
#ifdef _PLATFORM_IOS
    CAd_ShutDown();    
#endif 

#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdShutdown, 0);	
#endif
}

void     AdManager::Hide(void)
{
#ifdef _PLATFORM_IOS
    CAd_Hide();

#endif    

#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdCloseBanner, 0);	
#endif
}

void      AdManager::Show(void)
{
#ifdef _PLATFORM_IOS
    CAd_Show();    
#endif     

#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_AdCloseBanner, 0);	
#endif

}

void     AdManager::OnFullScreen()
{
    if(m_pHandler)
        m_pHandler->OnAdFullScreenShow();
}

void     AdManager::OnDismissed()
{
    if(m_pHandler)
        m_pHandler->OnAdFullScreenDismissed();
}


void     OnFullScreenCB()
{
    AdManager::Instance().OnFullScreen();
}


void     OnDismissCB()
{
    AdManager::Instance().OnDismissed();
}

